<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Http\Models\Student;

/**
 * Description of CourseBookingService
 *
 * @author Admin
 */

use App\Http\Models\Dances;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CourseBookingService
{

    public function index($request)
    {
        $where = [];
        if (Auth::user()['users_status'] != 1) {
            $where['teacher_id'] = Auth::user()['id'];
        }
        return CourseBooking::select()
            ->where($where)
            ->orderBy('created_at', 'desc')
            ->paginate(PAGES);
    }

    public function show($id)
    {
        return CourseBooking::where('id', decryptUrl($id))->first();
    }

    //取消预约
    public function remove($id)
    {
        try {
            DB::beginTransaction();
            $coureBookingData = $this->show($id);
            $dances = Dances::where('id', $coureBookingData->dance)->first();
            if ($dances) {
                if ($dances->types == 0) {
                    Dances::where('id', $coureBookingData->dance)->update([
                        'dance_status' => 0,
                    ]);
                } else {
                    Dances::where('id', $coureBookingData->dance)->decrement('league_subscribe_number', 1);
                }
            }
            CourseBooking::where('id', decryptUrl($id))->update([
                'status' => 1,
            ]);
            DB::commit();
            return 1;
        } catch (\Exception $exception) {
            DB::rollBack();
            return 0;
        }

    }

}
